package com.aaa.sbm.service.impl;

import com.aaa.sbm.common.ResultUtil;
import com.aaa.sbm.dao.SupplierDao;
import com.aaa.sbm.entity.Supplier;
import com.aaa.sbm.service.SupplierService;
import com.aaa.sbm.util.Page;
import com.aaa.sbm.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierDao supplierDao;

    @Override
    public Result getSupplierList(Page<Supplier> page) {
        System.out.println(page.getPageNo()+" ----- "+page.getPageSize());
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<Supplier> suppliers = supplierDao.selectList(page.getData());
        PageInfo<Supplier> supplierPageInfo = new PageInfo<>(suppliers);
        return ResultUtil.success(supplierPageInfo);
    }

    @Override
    public Result insertSupplier(Supplier supplier) {
        if (supplier == null) {
            return ResultUtil.fail("参数不能为空");
        }
        supplier.setStatus(1);
        int insert = supplierDao.insert(supplier);
        return insert > 0 ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @Override
    public Result updateSupplier(Supplier supplier) {
        if (supplier == null) {
            return ResultUtil.fail("参数不能为空");
        }
        int update = supplierDao.update(supplier);
        return update > 0 ? ResultUtil.success("修改成功") : ResultUtil.fail("修改失败");
    }

    @Override
    public Result deleteSupplier(Integer id) {
        if (id == null) {
            return ResultUtil.fail("参数不能为空");
        }
        int delete = supplierDao.deleteById(id);
        return delete > 0 ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @Override
    public Result listStatus() {
        List<Supplier> suppliers = supplierDao.selectByStatus();
        return ResultUtil.success(suppliers);
    }
}
